#include <iostream>
using namespace std;

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <cstring>
//信号捕捉
void handleSignal(int sig)
{
    printf("sig = %d\n",sig);
}

int main()
{   
#if 0
    //注册信号
    signal(SIGINT,handleSignal);
    //处理动作：
    //1. 忽略
    //2.默认动作
    //3.自定义动作
    while(1)
    {
        printf("hello world!\n");
        sleep(3);
    }
#endif
#if 0
    pid_t pid = fork();

    if(pid > 0)
    {   
        sleep(5);
        kill(pid,SIGKILL);
    }
    else if(pid == 0)
    {
        while(1)
        {
            printf("l am child process\n");
            sleep(1);
        }
    }
#endif
    pid_t pid = fork();
    if(pid > 0)
    {
        sleep(5);
        //发送信号
        kill(pid,SIGUSR1);
        //回收资源
        wait(NULL);
    }
    else if(pid == 0)
    {
        // signal(SIGUSR1,handleSignal);
        struct sigaction newAction;
        memset(&newAction,0,sizeof(newAction));
        newAction.sa_flags = 0;
        newAction.sa_handler = handleSignal;
        sigemptyset(&newAction.sa_mask);
        sigaction(SIGUSR1,&newAction,NULL);
        while(1)
        {
            cout<<"l am child process"<<endl;
            sleep(1);
        }
    }
    return 0;
}